package com.sskj.uboex.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.uboex.R;
import com.sskj.uboex.bean.NoticeList;
import com.sskj.uboex.presenter.MsgActivityPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.APP_MSG)//
public class MsgActivity extends BaseActivity<MsgActivityPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Autowired
    int type;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_msg;
    }

    @Override
    public MsgActivityPresenter getPresenter() {
        return new MsgActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        SparseArray<String> typeMap = new SparseArray<>();
        typeMap.put(0, "通知");
        typeMap.put(1, "公告");
        typeMap.put(2, "活动");
        setTitle(typeMap.get(type));
        setTitle("消息中心");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 25))
                .setRightPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 25))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.app_e6e))
        );
        SlimAdapter<NoticeList> slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_msg, new SlimInjector<NoticeList>() {
            @Override
            public void onInject(NoticeList data, IViewInjector injector, List payloads) {

                injector
                        .text(R.id.tvTitle, data.getTitle());
            }
        }).attachTo(recyclerView);
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, position, v) -> {
                    ARouter.getInstance().build(RConfig.APP_MSG_DETAIL).withString(Constans.ID, slimAdapter.getDataItem(position).getId()).navigation();
                });
        slimAdapter.updateData(new ArrayList());
        ModelRx2DataSource<NoticeList> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<NoticeList>)
                page -> mPresenter.getData(page + "", "20"), 20);
        MVCCoolHelper<List<NoticeList>> mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }
}
